package com.situ.springboot.config;

import com.situ.springboot.interceptor.LoginInterceptor;
import com.situ.springboot.pojo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Component  <bean></bean>
 * @Controller  @Service  @Repository
 * 这四个注解的作用是一样的
 *
 * @Configuration：用于定义配置类，可以替换xml配置文件
 * 加了这个注解的类的内部可以包含一个或者多个被@Bean注解的方法
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    // 配置虚拟路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**")
                .addResourceLocations("file:/D:/mypic/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    //<bean name="student1" class="com.situ.spring.pojo.Student"/>
    @Bean
    public Student createStudent() {
        Student student = new Student(1, "zhansgan", 23, "男");
        return student;
    }

    /*
    <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.situ.mvc.interceptor.MyInterceptor1"/>
		</mvc:interceptor>
	</mvc:interceptors>*/
    //这个方法用来注册拦截器，我们写的拦截器要在这里配置才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") //代表拦截所有请求
                .excludePathPatterns("/user/login", "/user/getUserLoginPage"); //排除哪些请求不需要拦截
    }
}
